package main

import (
	"io"
	"os"
	
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

// 极致的性能
var logger *zap.Logger

// 易用与性能相互权衡
var sugarLogger *zap.SugaredLogger

func main() {
	initLogger()
	defer sugarLogger.Sync()
	sugarLogger.Info("some message wile be write file")
}

func initLogger() {
	// 输出位置
	writeSyncer := getLogWriter()
	// 编码方式
	encoder := getEncoder()
	
	core := zapcore.NewCore(encoder, writeSyncer, zap.DebugLevel)
	
	logger = zap.New(core, zap.AddCaller())
	// logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
	sugarLogger = logger.Sugar()
}

func getLogWriter() zapcore.WriteSyncer {
	// _ = os.MkdirAll("./customize-logger/data.log", os.ModePerm)
	file, _ := os.OpenFile("./data_test/test.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	ws := io.MultiWriter(file, os.Stdout)
	return zapcore.AddSync(ws)
}

func getEncoder() zapcore.Encoder {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	return zapcore.NewJSONEncoder(encoderConfig)
}
